package com.sky.controller.user;

import com.github.pagehelper.Page;
import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController(value = "userOrdersController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端订单接口")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO dto){
        log.info("用户下单：{}",dto);
        OrderSubmitVO vo = ordersService.submit(dto);
        return Result.success(vo);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    /**
     * 查询历史订单
     */

    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result historyOrders(OrdersPageQueryDTO dto){
        log.info("查询历史订单:{}",dto);
        PageResult pageResult = ordersService.historyOrders(dto);
        log.info("查询结果：{}",pageResult);
        return Result.success(pageResult);
    }
    /**
     * 查询订单详情
     */
    @GetMapping("orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result getById(@PathVariable  Long id){
        log.info("查询订单详情：{}",id);
        //这里的OrdersHistoryDto是因为OrderDTO返回的其中列表的名字不对，所以这里用OrdersHistoryDto来接收
        Orders orders = ordersService.getById(id);
        return Result.success(orders);
    }
    /**
     * 取消订单
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id) throws Exception {
        log.info("取消订单：{}",id);
        ordersService.cancel(id);
        return Result.success();
    }
    /**
     * 再来一单
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result reOrder(@PathVariable  Long id){
        log.info("再来一单：{}",id);
        ordersService.reOrder(id);
        return Result.success();
    }
}

